package com.byq.mm.smartlib.application;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;


import com.byq.mm.abstractx.cluster.server.executor.Executor;
import com.byq.mm.abstractx.device.Cluster;
import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.engine.CoreEnvironment;
import com.byq.mm.application.cluster.provider.LevelControlProvider;
import com.byq.mm.application.cluster.provider.OnoffProvider;
import com.byq.mm.application.cluster.provider.PlayerProvider;
import com.byq.mm.smartlib.R;
import com.byq.mm.smartlib.component.SmartPlayer;
import com.byq.mm.smartlib.component.SoundManager;
import com.byq.mm.smartlib.cluster.server.AndroidExecutor;

import com.byq.mm.smartlib.engine.AndroidSmartEngine;
import com.byq.mm.smartlib.engine.NotificationHandler;
import com.byq.mm.smartlib.tools.NotificationHandlerImpl;

public class MySmartApplication extends SmartApplication {

	LocationListener locationListener;
	protected SoundManager sound;
	Map<String, Executor> executors = new HashMap<String, Executor>();
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	
		androidSmartEngine.getCoreEngine().getServerDeviceManager().reset(getLocalServerDevice());
		initExecutor();
		sound = new SoundManager(this);
		sound.addSound(0, R.raw.chimes);
		
	}
	public SmartPlayer getSound() {
		return sound;
	}
	public AndroidSmartEngine getAndroidSmartEngine() {
		return androidSmartEngine;
	}
	public void setLocationListener(LocationListener locationListener) {
		this.locationListener = locationListener;
	}
	void initExecutor(){
		Device root = androidSmartEngine.getCoreEngine().getServerDeviceManager().getLocalServerDevice();
		String deviceId = root.getId();
		for(Cluster cluster : root.getSupportedClusters()){
			Class<? extends AndroidExecutor> type = AndroidExecutor.getType(cluster.getId());
			if(type == null)
				continue;
			
			AndroidExecutor e;
			try {
				e = type.getConstructor(String.class, String.class, CoreEnvironment.class, Context.class).newInstance(deviceId, cluster.getId(), this.androidSmartEngine.getCoreEngine(), this);
				androidSmartEngine.getCoreEngine().getExecutorManager().register(deviceId, cluster.getId(), e);
				executors.put(cluster.getId(), e);
			} catch (Exception e1) {

				e1.printStackTrace();
			} 
			
		}
	}
	public Map<String, Executor> getRootLocalExecutors(){
		return executors;
	}
	public Device getLocalServerDevice() {
		Device me =new Device();
		me.setId("home");
		me.setType("home");
		me.setName("home");
		Cluster c = new Cluster();
		c.setId(com.byq.mm.cluster.provider.SimpleSensorProvider.Define.CLUSTER_NAME);
		me.getSupportedClusters().add(c);
		c = new Cluster();
		c.setId(LevelControlProvider.Define.CLUSTER_NAME);
		me.getSupportedClusters().add(c);
		
		c = new Cluster();
		c.setId(OnoffProvider.Define.CLUSTER_NAME);
		
		me.getSupportedClusters().add(c);
		c = new Cluster();
		c.setId(PlayerProvider.Define.CLUSTER_NAME);

		me.getSupportedClusters().add(c);
		return me;
	}
	public void stopListen(){
		LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		lm.removeUpdates(locationListener);

	}
	@Override
	public void onTerminate() {
		stopListen();
		super.onTerminate();
	}
	@Override
	protected Executor getExecutor(String device, String cluster) {
		Class<? extends AndroidExecutor> type = AndroidExecutor.getType(cluster);
		if(type == null)
			return null;
		AndroidExecutor e = null;
		try {
			e = type.getConstructor(String.class, String.class, CoreEnvironment.class, Context.class).newInstance(device, cluster, this.androidSmartEngine.getCoreEngine(), this);
			androidSmartEngine.getCoreEngine().getExecutorManager().register(device, cluster, e);
		} catch (Exception e1) {

			e1.printStackTrace();
		} 
		return e;
	}
	@Override
	public NotificationHandler getNotificationHandler() {
		// TODO Auto-generated method stub
		return new NotificationHandlerImpl(this);
	}

}
